package com.example.utils.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 文件处理工具类
 * 
 * @author lit
 */
public class FileUtils {
	public static List<List<String>> readFileByLines(String fileName) {
		List<List<String>> lineList = new ArrayList<List<String>>();
		
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			System.out.println("以行为单位读取文件内容，一次读一行");
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			int line = 1;
			// 一次读一行，读入null时文件结束
			while ((tempString = reader.readLine()) != null) {
				// 把当前行号显示出来
				System.out.println("line " + line + ": " + tempString);
				if(!"".equals(tempString)) {
					lineList.add(Arrays.asList(tempString.split(" ")));
				}
				line++;
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();

				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return lineList;
	}

	public static void main(String[] args) {
//		List<List<String>> lineList = readFileByLines("D:\\workspaceSVN\\ec9_back\\src\\main\\resources\\static\\a");
//		System.out.println(lineList);
//		for (List<String> list : lineList) {
//			String sql = "insert into third_table_area(name,longitude1,longitude2,latitude1,latitude2) "
//					+ "values(\""+list.get(0).trim()+"\",\""+list.get(1).split("~")[0].trim()+"\",\""+list.get(1).split("~")[1].trim()+"\",\""+list.get(2).split("~")[0].trim()+"\",\""+list.get(2).split("~")[1].trim()+"\");";
//			System.out.println(sql);
//		}
		
		List<List<String>> a = readFileByLines("D:\\workspaceSVN\\ec9_back\\src\\main\\resources\\static\\b");
		System.out.println(a);
		for (List<String> list : a) {
			String province = list.get(0).split(":")[0];
			List<String> cityList = new ArrayList<String>();
			for (String city : list) {
				if (!city.contains(":")) {
					cityList.add(city);
				}
			}
			String sql = "insert into third_table_province(province,city) "
					+ "values(\""+province+"\",\""+cityList+"\");";
			System.out.println(sql);
		}
	}
}
